//
//  RzViewController.m
//  zhihuiguanjia
//
//  Created by apple on 16/4/3.
//  Copyright © 2016年 MyCompany. All rights reserved.
//

#import "RzViewController.h"

@interface RzViewController ()

@property (nonatomic, strong) NSMutableArray *ypList;
@property (nonatomic, strong) UIButton *back;
@property (nonatomic, strong) NetworkManager *networkManager;
@property (nonatomic, strong) MBProgressHUD *HUD;
@property (retain, nonatomic) IBOutlet UITableView *tableView; 
@property (nonatomic, assign) NSInteger selectedRow;

@end
@implementation RzViewController

- (void)viewDidLoad
{
    [self.navigationController setNavigationBarHidden:YES];
    self.ypList = [NSMutableArray array];
    [super viewDidLoad];
    _tableView.frame  = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [[self view] addSubview:[self tableView]];
    self.networkManager = [[NetworkManager alloc] init];
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self getCq];
    
    [self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
}

- (void)getCq {
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           _zhuhuId,@"zhuhu_id", nil];
    [self.networkManager requestWithMethod:ruzhu parameter:param delegate:self];
    [self.HUD show:YES];
    
}


- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    if (methodId == ruzhu) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            
            if([result isEqualToString:@"000"]){
                
                [self.ypList removeAllObjects];
                
                NSArray *results = [data objectForKeyOrNil:@"ruzhulist"];
                if (![results isKindOfClass:[NSNull class]] && [results count] > 0) {
                    [self.ypList  addObjectsFromArray:results];
                    NSLog(@"%lu",(unsigned long)[self.ypList count]);
                    [self.tableView reloadData];
                } else {
                    UILabel *zxlabel = [[UILabel alloc] initWithFrame:CGRectMake(20,20,200,10)];
                    zxlabel.text = @"暂无入住信息";
                    [self.view addSubview:zxlabel];
                     BTToast(@"暂无入住信息");
                }
                
                
            } else if([result isEqualToString:@"001"]){
                NSString * resultinfo = [data objectForKeyOrNil:@"resultinfo"];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                                message:[resultinfo stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                                                               delegate:nil
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
                [alert show];
                
            }
        }
    }
}

#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%lu",(unsigned long)[self.ypList count]);
    return [self.ypList count]>1?1:[self.ypList count];
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFailedWithError:(NSError *)error {
    NSDictionary *errorInfo = error.userInfo;
    NSString *msg = [errorInfo objectForKeyOrNil:@"errorMsg"];
    
    NSInteger code = error.code;
    if (code != 1) {
        if ([msg length] == 0) {
            msg = @"获取失败，请稍后重试";
        }
    }
    
    if ([msg length] > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *RuzhuCell = @"RuzhuCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             RuzhuCell];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:RuzhuCell];
    }
    if (self.ypList != nil && self.ypList.count >0) {
        NSUInteger row = [indexPath row];
        UILabel *ryxmLbl = (UILabel *)[cell.contentView viewWithTag:100];
        UILabel *lxdhLbl = (UILabel *)[cell.contentView viewWithTag:101];
        NSDictionary* dic = [self.ypList objectAtIndex:row];
        
        
        NSString *ryxmStr = [[dic objectForKey:@"ryxm"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        if([[dic objectForKey:@"rygx"] isEqualToString:@"户主"]){
            ryxmLbl.text = ryxmStr;
            lxdhLbl.text = [dic objectForKey:@"lxdh"];
        }
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSInteger row = indexPath.row;
    self.selectedRow = row;
    
    if ([self.ypList count] > row) {
        if ([self.ypList count] > row) {
            NSDictionary *wzInfo = [self.ypList objectAtIndex:row];
            NSString *phone = [wzInfo objectForKey:@"lxdh"] ;
            NSString *num = [[NSString alloc]initWithFormat:@"telprompt://%@",phone]; //而这个方法则打电话前先弹框 是否打电话 然后打完电话之后回到程序中 网上说这个方法可能不合法 无法通过审核
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSIndexPath *)indexPath{
    return 0.5;
}

@end

